﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cs499.Admin
{
    public partial class CreateDepartment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["CreateDepartmentLabelText"] != null && IsPostBack)
            {
                CreateDepartmentLabel.Text = (string)HttpContext.Current.Session["CreateDepartmentLabelText"];
                //OKButton.Enabled = false;
            }
            else if (IsPostBack)
            {
                CreateDepartmentLabel.Text = "";
            }
            else
            {
                CreateDepartmentLabel.Text = "";
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton adminMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("AdminMenuButton");
            adminMenuButton.CssClass = "selected";

            if (CreateDepartmentLabel.Text != "") return;
            CreateDepartmentLabel.Text = "Input information for new Department";
        }

        
        protected void OKButton_Click(object sender, EventArgs e)
        {
            String departmentcode = inputdepartmentcode.Text;
            String departmentname = inputdepartmentname.Text;

            if (departmentcode == "")
            {
                String errmsg = "Department Code cannot be blank";
                if (CreateDepartmentLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["CreateDepartmentLabelText"] = errmsg;
                    Server.Transfer("~/Admin/CreateDepartment.aspx", true);
                }
                return;
            }
            if (departmentname == "")
            {
                String errmsg = "Department Name cannot be blank";
                if (CreateDepartmentLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["CreateDepartmentLabelText"] = errmsg;
                    Server.Transfer("~/Admin/CreateDepartment.aspx", true);
                }
                return;
            }
            cs499.Database.Department newDepartment=cs499.Database.ListOfDepartment.CreateDepartment(departmentname, departmentcode);
            if (newDepartment == null)
            {
                String errmsg = "Department Name and Code must be unique";
                if (CreateDepartmentLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["CreateDepartmentLabelText"] = errmsg;
                    Server.Transfer("~/Admin/CreateDepartment.aspx", true);
                }
                return;
            }
            else
            {
                HttpContext.Current.Session["CreateDepartmentLabelText"] = "";
                CreateDepartmentLabel.Text = "Success";
                Response.Redirect("~/Admin/ListUsers.aspx?action=Department%20Created%20Successfully");
            }
        }
    }
}